package com.drools.demo.server;

import com.drools.demo.entity.Person;
import lombok.Setter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author czq
 * @Package com.drools.demo.server
 * @Date 2024/8/27 14:58
 * @description:
 */
@Service
public class ListService {
    //规则引擎转发业务逻辑
    @Setter
    private List<String> afterList = new ArrayList<>();
    @Resource
    private KieContainer kieContainer;

    public void calculate(List<String> list) {
        KieSession kieSession = kieContainer.newKieSession();
        kieSession.insert(list);
        kieSession.setGlobal("listService", this);
        kieSession.fireAllRules();
        kieSession.dispose();
    }

    /**
     * 获取筛选后的数据
     */
    public List<String> getAfterFilterElements(){
        return afterList.stream().filter(l -> !l.equals("666")).collect(Collectors.toList());
    }
}
